﻿@inherits RootLayout
@inject NavigationManager navigationManager
@inject AppState appState
@inject IStringLocalizer<Global> L

<CascadingValue Value="this">
    <MatDrawerContainer Style="width: 100vw; height: 100vh;" Class="@bbDrawerClass">
        <MatDrawer @bind-Opened="@_navMenuOpened">
            <header class="drawer-header">
                <div class="drawer-logo">
                    <img alt="@appState.AppName" class="logo-img" src="_content/BlazorBoilerplate.Theme.Material/images/logo-dark.svg" title="@appState.AppName">
                    <a class="miniHover" href="/">@appState.AppName</a>
                </div>
                <UserProfile />
            </header>
            <NavMenu />
            <DrawerFooter></DrawerFooter>
        </MatDrawer>
        <MatDrawerContent>
            <MatAppBarContainer Style="display: flex; flex-direction: column; min-height: 100vh;">
                <MatAppBar Fixed="true">
                    <MatAppBarRow>
                        <MatAppBarSection>
                            <MatAppBarTitle>
                            </MatAppBarTitle>
                            <div class="hidden-mdc-down">
                                <MatTooltip Tooltip="@L["AppHoverNavToggle"]">
                                    <MatIconButton Class="navToggle" Icon="menu" ToggleIcon="menu" OnClick="@((e) => NavToggle())" RefBack="@context"></MatIconButton>
                                </MatTooltip>
                                <MatTooltip Tooltip="@L["AppHoverNavMinimize"]">
                                    <MatIconButton Class="navToggle" Icon="format_indent_decrease" ToggleIcon="format_indent_increase" OnClick="@((e) => NavMinify())" RefBack="@context"></MatIconButton>
                                </MatTooltip>
                                @*</li>
                                    <li>
                                        <MatTooltip Tooltip="Lock Navigation">
                                            <MatIconButton Class="navToggle" Icon="lock" ToggleIcon="radio_button_unchecked" OnClick="@((e) => NavLock())" RefBack="@context"></MatIconButton>
                                        </MatTooltip>
                                        </li>*@
                            </div>
                        </MatAppBarSection>
                        <MatAppBarSection Align="@MatAppBarSectionAlign.End">
                            <TopRightBarSection></TopRightBarSection>
                        </MatAppBarSection>
                    </MatAppBarRow>
                </MatAppBar>
                <MatAppBarContent Style="flex: 1; display: flex; flex-direction: column;">
                    @TopSection
                    <section class="container-fluid" style="flex: 1; display: flex; flex-direction: column;">
                        @Body
                    </section>
                    <Footer></Footer>
                </MatAppBarContent>
            </MatAppBarContainer>
        </MatDrawerContent>
    </MatDrawerContainer>
</CascadingValue>

@code {
    bool _navMenuOpened = true;
    bool _navMinified = false;
    public string bbDrawerClass = string.Empty;

    [CascadingParameter]
    Task<AuthenticationState> authenticationStateTask { get; set; }

    protected override async Task OnInitializedAsync()
    {
        var user = (await authenticationStateTask).User;

        if (user.Identity.IsAuthenticated)
        {
            var profile = await appState.GetUserProfile();

            _navMenuOpened = profile.IsNavOpen;
            _navMinified = profile.IsNavMinified;
        }
    }

    void NavToggle()
    {
        _navMenuOpened = !_navMenuOpened;

        if (_navMenuOpened)
        {
            bbDrawerClass = "full";
        }
        else
        {
            bbDrawerClass = "closed";
        }

        StateHasChanged();
    }

    void NavLock()
    {
        //Todo Lock Nav
    }

    void NavMinify()
    {
        _navMinified = !_navMinified;

        if (!_navMenuOpened)
        {
            _navMinified = true;
        }

        if (_navMinified)
        {
            bbDrawerClass = "mini";
            _navMenuOpened = true;
        }
        else if (_navMenuOpened)
        {
            bbDrawerClass = "full";
        }

        _navMenuOpened = true;

        StateHasChanged();
    }
}
